package com.learn.xml_config.third_bean;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ThirdBean {
    public static void main(String[] args) throws SQLException, ClassNotFoundException, ParseException {
        // 非Spring配置
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/learn_sql");
        dataSource.setUsername("root");
        dataSource.setPassword("makabaka");
        System.out.println(dataSource);
        
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/learn_sql", "root", "makabaka");
        System.out.println(connection);
        
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse("2025-02-19 12:00:00");
        System.out.println(date);
        
        // Spring配置
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xml-config/third-bean.xml");
        DruidDataSource dataSource1 = applicationContext.getBean("dataSource", DruidDataSource.class);
        System.out.println(dataSource1);
        
        Connection connection1 = applicationContext.getBean("connection", Connection.class);
        System.out.println(connection1);
        
        Date date1 = applicationContext.getBean("date", Date.class);
        System.out.println(date1);
    }
}
